import javax.swing.*;

/**
 * The type Food collection.
 *
 * @author Yihan Wang
 * @date 24/06/09
 */
public class FoodCollection {
    private ALSet<Food> foodSet;
    private final JTextArea displayArea;

    /**
     * Instantiates a new Food collection.
     */
////////////////////////////////////////////////
    public FoodCollection() {
        foodSet = new ALSet<>();
        this.displayArea = new JTextArea(10, 20);
    }

    /**
     * Instantiates a new Food collection.
     *
     * @param foodSet the food set
     */
    public FoodCollection(ALSet<Food> foodSet) {
        this.foodSet = foodSet;
        this.displayArea = new JTextArea(10, 20);
    }
    ////////////////////////////////////////////////

    /**
     * Gets food set.
     *
     * @return the food set
     */
    public ALSet<Food> getFoodSet() {
        return foodSet;
    }

    /**
     * Sets food set.
     *
     * @param foodSet the food set
     */
    public void setFoodSet(ALSet<Food> foodSet) {
        this.foodSet = foodSet;
    }
    ////////////////////////////////////////////////

    /**
     * Contains food.
     *
     * @param name the name
     * @return the food (null means not found)
     */
    public Food containsFood(String name) {
        for (Food food : foodSet) {
            if (food.getName().equalsIgnoreCase(name)) {
                return food;
            }
        }
        return null;
    }

    /**
     * Add food.
     *
     * @param name   the name
     * @param amount the amount
     * @param unit   the unit
     */
    public void addFood(String name, double amount, String unit) {
        Food tempFood = containsFood(name);
        if (tempFood != null) {
            tempFood.addAmount(amount);
        } else {
            Food newFood = new Food(name, amount, unit);
            this.foodSet.add(newFood);
        }
    }

    /**
     * Remove food string.
     *
     * @param name   the name
     * @param amount the amount
     * @param unit   the unit
     * @return the string to hint user
     */
    public String removeFood(String name, double amount, String unit) {
        Food tempFood = containsFood(name);
        if (tempFood != null) {
            if (tempFood.getAmount() >= amount) {
                tempFood.decreaseAmount(amount);
                return "You remove " + amount + " " + unit + " " + name;
            } else {
                return "The ingredients are not stored enough to remove";
            }
        } else {
            return "There's no such ingredient in the fridge";
        }
    }

    /**
     * Print all food info string.
     *
     * @return the string
     */
    public String printAllFoodInfo() {
        StringBuilder result = new StringBuilder("============== Refrigerator ============\n");
        for (Food food : foodSet) {
            if (food.getAmount() <= 0) {
                continue;
            }
            result.append(food.getFoodInfo());
            result.append("\n");
        }
        return result.toString();
    }

    /**
     * Check missing food info.
     *
     * @param r the r
     * @return the string
     */
    public String checkMissingFood(Recipe r) {
        StringBuilder missingFoodsInfo = new StringBuilder();
        for (Recipe.Ingredient igt : r.getIngredients()) {
            Food food = containsFood(igt.getName());
            if (food == null || food.getAmount() < igt.getQuantity()) {
                double missingAmount = (food == null) ? igt.getQuantity() : igt.getQuantity() - food.getAmount();
                missingFoodsInfo.append(igt.getName() + " x " + missingAmount + " " + igt.getUnit() + "\n");
            }
        }
        return missingFoodsInfo.toString();
    }

    /**
     * Clean collection.
     */
    public void cleanCollection() {
        foodSet = new ALSet<>();
    }
}
